package com.itheima.common.advice;

import com.itheima.common.exception.IhrmException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.Arrays;

/**
 * @author IntelliJ IDEA
 * @date 2020/10/18  11:40
 */
@Aspect
@Component
@Slf4j
public class CommonLogAdvice {
    @Around("within(@org.springframework.stereotype.Service *)")
    public Object hanlderExceptionLog(ProceedingJoinPoint jp){
        try {
            log.debug("{}方法准备执行",jp.getSignature());
            long begin = System.currentTimeMillis();
            Object result = jp.proceed();
            long end = System.currentTimeMillis();
            log.debug("{}方法执行了，参数为:{},共计耗时{}毫秒",jp.getSignature(), Arrays.toString(jp.getArgs()),(end-begin));
            return result;
        } catch (Throwable throwable) {
            log.error("{}方法出现异常，异常信息为:{}",jp.getSignature(),throwable.getMessage());
            if(throwable instanceof IhrmException){
                throw (IhrmException)throwable;
            }else{
                throw new IhrmException(throwable, 500);
            }
        }
    }
}
